#include "widgetinversionform.h"
#include "ui_widgetinversionform.h"
#include <QSplineSeries>
#include <QDateTimeAxis>
#include <QValueAxis>
#include <QDateTime>
#include <QLegendMarker>

WidgetInversionForm::WidgetInversionForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::WidgetInversionForm)
{
    ui->setupUi(this);

    this->InitChart_Temperature_Humidity();
}

WidgetInversionForm::~WidgetInversionForm()
{
    delete ui;
}

void WidgetInversionForm::InitChart_Temperature_Humidity()
{
    m_chart = new QChart();

    QSplineSeries* m_series = new QSplineSeries(m_chart);
    m_series->setPen(QPen(QBrush(Qt::blue),2, Qt::PenStyle::SolidLine,
                                      Qt::PenCapStyle::RoundCap, Qt::PenJoinStyle::RoundJoin));

    m_chart->addSeries(m_series);


    // 更改标题颜色和字体大小
    QFont titleFont("sans-serif", 12, QFont::Bold);
    m_chart->setTitleFont(titleFont);
    QBrush titleBrush(Qt::black);
    m_chart->setTitleBrush(titleBrush);

    // 设置y轴
    QValueAxis *axisY_tem = new QValueAxis();
    axisY_tem->setRange(0, 50);
    axisY_tem->setTickCount(6);
    axisY_tem->setLabelFormat("%.2lf");

    // 设置x轴
    QDateTimeAxis* axisX = new QDateTimeAxis;
    axisX->setTickCount(5);
    axisX->setFormat("hh:mm:ss");
    axisX->setRange(QDateTime::currentDateTime().addSecs(-60), QDateTime::currentDateTime().addMSecs(500));

    m_chart->addAxis(axisX, Qt::AlignBottom);
    m_chart->addAxis(axisY_tem, Qt::AlignmentFlag::AlignLeft);

    m_series->attachAxis(axisX);
    m_series->attachAxis(axisY_tem);


    m_chart->legend()->markers().at(0)->setVisible(true);
    m_chart->legend()->markers().at(0)->setFont(QFont("微软雅黑",12));
    m_chart->legend()->markers().at(0)->setPen(QPen(Qt::blue));
    m_series->setName("透过率");

    m_chart->legend()->attachToChart();

    ui->graphicsView_tgl->setChart(m_chart);
    ui->graphicsView_tgl->setRenderHint(QPainter::Antialiasing);

    /*
    connect(this, &InitChart_Temperature_Humidity::updateView, this, [&](int temperature){

        m_series->append(QDateTime::currentDateTime().toMSecsSinceEpoch(), temperature);
        if(m_series->count() > 100){
            m_series->removePoints(0, m_series->count() - 100);
        }

        QDateTime start1 = axisX->min().addMSecs(1000);
        QDateTime end1 = axisX->max().addMSecs(1000);
        axisX->setRange(start1, end1);
        ui->graphicsView_tgl->update();
    });
*/
}
